package com.xiaoc.bulldozer.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class IdentityNoUtil {

    /**
     * 根据身份证的号码算出当前身份证持有者的性别和年龄 18位身份证
     *
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getCarInfo(String identityNo) {

        Map<String, Object> map = new HashMap<String, Object>();
        String year = identityNo.substring(6).substring(0, 4);// 得到年份
        String month = identityNo.substring(10).substring(0, 2);// 得到月份
        String day = identityNo.substring(12).substring(0, 2);// 得到日
        String sex;
        if (Integer.parseInt(identityNo.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
            sex = "女";
        } else {
            sex = "男";
        }

        Date date = new Date();// 得到当前的系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String currentYear = format.format(date).substring(0, 4);// 当前年份
        String currentMonth = format.format(date).substring(5, 7);// 月份
        String currentDay = format.format(date).substring(8, 10);// 月份

        int age = 0;
        if (Integer.parseInt(month) <= Integer.parseInt(currentMonth) && Integer.parseInt(day) < Integer.parseInt(currentDay)) {

            age = Integer.parseInt(currentYear) - Integer.parseInt(year) + 1;
        } else {
            age = Integer.parseInt(currentYear) - Integer.parseInt(year);
        }
        map.put("sex", sex);
        map.put("age", age);
        return map;
    }


}
